﻿using System.Windows;
using FieldTools.Client.View;
using FieldTools.Client.ViewModel;
using FieldTools.Util;
using MahApps.Metro.Controls.Dialogs;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Serilog;
using Serilog.Events;
using System;
using System.IO;
using Serilog.Core;
using FieldTools.Client.View.Base;
using FieldTools.Client.View.Window;

namespace FieldTools.Client
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        public static ServiceProvider serviceProvider;
        public static ServiceCollection services;
        protected override void OnStartup(StartupEventArgs e)
        {
            #region Serilog的配置
            var config = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                //.AddJsonFile($"appsettings.{environment}.json", optional: true, reloadOnChange: true)
                .Build();

            Log.Logger = new LoggerConfiguration()
                .ReadFrom.Configuration(config)
                //.Enrich.With(new SourceContextEnricher())
                .WriteTo.Console(theme: Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme.Code)
                .CreateLogger();

            #endregion

            services = new ServiceCollection();
            //日志
            services.AddLogging(builder => builder.AddSerilog(Log.Logger, dispose: true));
            //自动注入特定生命周期对象
            services.AddFxServicesMini();
            //任务管理
            services.AddTaskServices();
            serviceProvider = services.BuildServiceProvider();

            //创建控制台并关闭，以确保控制台资源得到初始化，且不弹出额外的窗口
            ConsoleWindow.InitConsole();
            //登录
            //LoginWindow login = serviceProvider.GetRequiredService<LoginWindow>();
            //login.Show();
            //跳过登录
            ToolsBox box = serviceProvider.GetRequiredService<ToolsBox>();
            box.Show();
            //测试页面窗口
            //BasePageWindow pageWindow = new();
            //pageWindow.Show();
        }
    }

    //使用扩展器将线程ID附加到每个事件，可以在输出内容加入
    //例：outputTemplate: "{Timestamp:HH:mm} [{Level}] ({SourceContext}) {Message}{NewLine}{Exception}")
    //public class SourceContextEnricher : ILogEventEnricher
    //{
    //    public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
    //    {
    //        logEvent.AddPropertyIfAbsent(propertyFactory.CreateProperty(
    //                "SourceContext:1", System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name));
    //    }
    //}
}
